package cn.xiaoming.car.server.account.dao.persist.repository.impl;

import cn.xiaoming.car.server.account.dao.persist.mapper.AdminMapper;
import cn.xiaoming.car.server.account.dao.persist.repository.IAdminRepository;
import cn.xiaoming.car.server.account.pojo.entity.Admin;
import cn.xiaoming.car.server.account.pojo.vo.AdminListItemVO;
import cn.xiaoming.car.server.account.pojo.vo.AdminStandardVO;
import cn.xiaoming.car.server.common.pojo.vo.PageData;
import cn.xiaoming.car.server.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class AdminRepositoryImpl implements IAdminRepository {
    @Autowired
    AdminMapper adminMapper;

    public AdminRepositoryImpl() {
        log.info("创建存储库对象：AdminRepositoryImpl");
    }

    @Override
    public int insert(Admin admin) {
        log.debug("开始执行【插入管理员】的数据访问，参数：{}", admin);
        return adminMapper.insert(admin);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【删除管理员】的数据访问，参数：{}", id);
        return adminMapper.deleteById(id);
    }

    @Override
    public int updateById(Admin admin) {
        log.debug("开始执行【更新用户】的数据访问，参数：{}", admin);
        return adminMapper.updateById(admin);
    }

    @Override
    public int countByUsername(String username) {
        log.debug("开始执行【根据用户名统计管理员数据的数量】的数据访问，参数：{}", username);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("开始执行【根据用户名统计管理员数据的数量】的数据访问，参数：{}", phone);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", phone);
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmail(String email) {
        log.debug("开始执行【根据用户名统计管理员数据的数量】的数据访问，参数：{}", email);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", email);
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public AdminStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询管理员详情】的数据访问，参数：{}", id);
        return adminMapper.getStandardById(id);
    }

    @Override
    public PageData<AdminListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询用户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AdminListItemVO> list = adminMapper.list();
        PageInfo<AdminListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AdminListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
